/**
 * Module dependencies.
 */

var util = require('util');
var objDumper = require('./Util/Dumper.js');
var World = require('./WorldModel/World.js');
var GameRule = require('./GameRule');

var express = require('express')
  , routes = require('./routes')
  , user = require('./routes/user')
  , administration = require('./routes/administration')
  , dummy = require('./routes/dummy')
  , http = require('http')
  , path = require('path')
  , connect = require('connect')
  , io = require('socket.io')
  , auth = require('./security/auth')
  , EventManager = require('./event/EventManager.js')
  , cookieParser = express.cookieParser('w5hP4qnnRnEebVN2rRIoehEgGZ7f9n0wEJylFpmi9bRts4rPSUrDGOexKPJ2DeK')
  , sessionStore = new connect.middleware.session.MemoryStore();

var app = express();


app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'ejs');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(cookieParser);
  app.use(express.session({store: sessionStore}));
  app.use(app.router);
  app.use(express.static(path.join(__dirname, 'public')));
});

app.configure('development', function(){
  app.use(express.errorHandler());
});


app.all('/', auth.requireLoggin,routes.index);
app.all('/login', user.login);
app.all('/register', user.register);
app.all('/validate/:token', user.validateAccount);
app.all('/resetPassword', user.resetPassword);
app.all('/passwordRedefinition/:token', user.passwordRedefinition);

app.all('/choosePersonage', auth.requireLoggin,user.choosePersonage);

app.all("/dummyMap", dummy.generateMap);

app.all('/logout', function (req, res) {
    delete req.session.user;
    res.redirect('/login');
});

app.all('/administration', auth.requireLoggin,user.administration);
app.all('/administration/weapon/edit/:id', auth.requireLoggin,administration.editWeapon);
app.all('/administration/weapon/delete/:id', auth.requireLoggin,administration.deleteWeapon);
app.all('/administration/weaponIndex',auth.requireLoggin, administration.index);
app.all('/administration/weapon/create', auth.requireLoggin,administration.createWeapon);
app.all('/administration/plantation/create', auth.requireLoggin,administration.createPlantation);
app.all('/administration/plantationIndex',auth.requireLoggin, administration.plantationIndex);
app.all('/administration/maturity/create', auth.requireLoggin,administration.createMaturity);
app.all('/administration/maturityIndex',auth.requireLoggin, administration.maturityIndex);
app.all('/administration/maturity/edit/:id', auth.requireLoggin,administration.editMaturity);
app.all('/administration/maturity/delete/:id', auth.requireLoggin,administration.deleteMaturity);




var server = http.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});


var SessionSockets = require('session.socket.io')
    , sio = io.listen(server)
    , sessionSockets = new SessionSockets(sio, sessionStore, cookieParser)

global.GameRules = new GameRule();
console.log(global.GameRules);
//util.inherits(ClientWorld, World);

var eventManager = new EventManager(sessionSockets);